package com.hualan.servlet;

import com.alibaba.fastjson.JSON;
import com.hualan.dao.PanghuGoodsShangJiaDao;
import com.hualan.dao.PanghuGoodsShangJiaDaoImpl;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
//头部查询按钮 ！！全部上架  / 全部下架 商品
@WebServlet("/goods/shangxiaJia")
public class PanghuGoodsShangJiaServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("application/json;charset=UTF-8");
        req.setCharacterEncoding("utf-8");

        //新建PanghuGoodsShangJiaDaoImpl对象
        PanghuGoodsShangJiaDao panghuGoodsShangJiaDao=new PanghuGoodsShangJiaDaoImpl();
        //获取前端查询的goodsStatus数据  1 / 0？
        String goodsStatusStr = req.getParameter("goodsStatus");
        int goodsStatus = Integer.parseInt(goodsStatusStr);

        //存储goodsStatus == 1 / 0 ？ 的商品数据
        Map<String, Object> map = new HashMap<>();
        //判断所有上架商品
        if (goodsStatus == 1){
            List shangJia = panghuGoodsShangJiaDao.selectShangJia();
            map.put("list",shangJia);
            resp.getWriter().write(JSON.toJSONString(map));
            //判断所有下架商品
        } else if (goodsStatus == 0) {
            List xiaJia = panghuGoodsShangJiaDao.selectXiaJia();
            map.put("list",xiaJia);
            resp.getWriter().write(JSON.toJSONString(map));
        }else{
            resp.getWriter().write("status不合法！！！");
        }
    }
}
